package com.atguigu.ucenterService.controller;


import com.atguigu.commonUtils.Result;
import com.atguigu.commonUtils.ResultUtil;
import com.atguigu.commonUtils.vo.UcenterMemberVo;
import com.atguigu.ucenterService.entity.UcenterMember;
import com.atguigu.ucenterService.service.UcenterMemberService;
import com.atguigu.ucenterService.vo.RegisterVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author Chen Wen bo
 * @since 2021-08-12
 */
@Api(tags = "用户操作")
@RestController
@RequestMapping("/ucenterService/member")
@CrossOrigin
public class UcenterMemberController {

    @Resource
    private UcenterMemberService ucenterMemberService;

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result<HashMap<String, String>> login(@RequestBody UcenterMember ucenterMember){
        HashMap<String, String> map = new HashMap<>();
        String token = ucenterMemberService.login(ucenterMember);
        map.put("token",token);
        return ResultUtil.success(map);
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public Result<RegisterVo> register(@RequestBody RegisterVo registerVo){
        ucenterMemberService.register(registerVo);
        return ResultUtil.success();
    }

    @ApiOperation("根据token获取用户信息")
    @GetMapping("/getMemberInfo")
    public Result<UcenterMember> getMemberInfo(HttpServletRequest request){
        UcenterMember ucenterMember = ucenterMemberService.getMemberInfo(request);
        return ResultUtil.success(ucenterMember);
    }

    @ApiOperation("根据用户id获取用户信息")
    @GetMapping("/getMember/{id}")
    public UcenterMemberVo getMember(@PathVariable String id){
        UcenterMember member = ucenterMemberService.getById(id);
        UcenterMemberVo ucenterMemberVo = new UcenterMemberVo();
        BeanUtils.copyProperties(member, ucenterMemberVo);
        return ucenterMemberVo;
    }
}

